﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

namespace Mantenimiento.Negocio
{
    [Serializable]
    public class Usuario
    {

        #region CAMPOS
        private bool bNuevo;
        private string login;
        private string email;
        private List<String> lRoles;
        private bool bAdministrador;
        private string password;
        #endregion

        #region PROPIEDADES
        public string Login
        {
            get { return login; }
            set { login = value; }
        }
        public List<String> LRoles
        {
            get { return lRoles; }
            set { lRoles = value; }
        }
        public bool BAdministrador
        {
            get { return bAdministrador; }
            set { bAdministrador = value; }
        }
        public string Email
        {
            get { return email; }
            set { email = value; }
        }
        public bool BNuevo
        {
            get { return bNuevo; }
            set { bNuevo = value; }
        }
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        #endregion

        #region CONSTRUCTORES
        public Usuario()
        {
            bNuevo = true;
        }
        public Usuario(MembershipUser mU)
        {
            bNuevo = false;
            bAdministrador = false;
            login = mU.UserName;
            lRoles = Roles.GetRolesForUser(mU.UserName).ToList();
            email = mU.Email;
            //password = mU.GetPassword();
            foreach (string s in lRoles)
            {
                if (s.ToLower() == "administrador")
                    bAdministrador = true;
            }
        }
        #endregion

        #region METODOS PRIVADOS


        // Search predicate returns true if a string ends in "saurus".
        private static bool esAdministrador(String s)
        {
            if ((s.Length > 5) &&
                (s.Substring(s.Length - 6).ToLower() == "saurus"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region METODOS PUBLICOS
        public void guardar()
        {
            if (bNuevo)
            {
                MembershipCreateStatus mcsStatus;
                Membership.CreateUser(login, password, email, "Quien es quien?", "quien?", true, out mcsStatus);
            }
            else
            {
                MembershipUserCollection Coleccion = Membership.FindUsersByName(login);
                if (Coleccion.Count == 1)
                {
                    Coleccion[login].Email = email;
                    Membership.UpdateUser(Coleccion[login]);
                }
            }
        }
        public void eliminar()
        {
            Membership.DeleteUser(login);            
        }
        public void establecerRol(string sRol)
        {
            Roles.AddUserToRole(login, sRol);
        }
        public void quitarRol(string sRol)
        {
            Roles.RemoveUserFromRole(login, sRol);
        }
        public void establecerRolAdmin()
        {
            establecerRol("Administrador");
        }
        public void quitarRolAdmin()
        {
            quitarRol("Administrador");
        }
        public bool establecerPassword(string sPasswordOld, string sPasswordNew)
        {
            MembershipUserCollection Coleccion = Membership.FindUsersByName(login);
            return Coleccion[login].ChangePassword(sPasswordOld, sPasswordNew);
        }
        #endregion

        #region METODOS ESTATICOS DE CLASE
        public static void crearUsuario(string sUsuario, string sPassword, string sEmail, string sPassQuestion, string sPassAnswer, bool bIsApproved)
        {
            MembershipCreateStatus mcsStatus;
            Membership.CreateUser(sUsuario, sPassword, sEmail, sPassQuestion, sPassAnswer, bIsApproved, out mcsStatus);
        }

        public static void crearUsuario(string sUsuario, string sPassword, string sEmail, string sPassQuestion, string sPassAnswer, bool bIsApproved, out MembershipCreateStatus mcsStatus)
        {
            Membership.CreateUser(sUsuario, sPassword, sEmail, sPassQuestion, sPassAnswer, bIsApproved, out mcsStatus);
        }

        public static List<Usuario> obtenerUsuarios()
        {
            List<Usuario> lista = new List<Usuario>();
            foreach (MembershipUser mU in Membership.GetAllUsers())
            {
                lista.Add(new Usuario(mU));
            }
            return lista;
        }

        public static Usuario obtener(string sLogin)
        {
            MembershipUserCollection Coleccion = Membership.FindUsersByName(sLogin);
            return new Usuario(Coleccion[sLogin]); ;
        }
        #endregion
    }
}
